﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic;

namespace EasyStudy.UI
{
    public partial class CtrlTodoTaskEx : UserControl
    {
        private int count = 1;
        private int totalStep = 50;
        private Boolean direction = true;           // true往前、false往后
        private Color color1 = Color.FromArgb(239, 101, 150);
        private Color color2 = Color.FromArgb(34, 181, 244);

        private Color defaultBtnBackColor = Color.Transparent;

        public CtrlTodoTaskEx()
        {
            InitializeComponent();

            buttonSpecHeaderGroup1.ButtonSpecPropertyChanged +=
                new PropertyChangedEventHandler(buttonSpecHeaderGroup1_ButtonSpecPropertyChanged);

            this.timerColorChange.Interval = 30;
            this.timerColorChange.Tick += new EventHandler(timerColorChange_Tick);

            defaultBtnBackColor = btnCopy.StateCommon.Back.Color1;
        }

        /// <summary>
        /// 开始变换背景颜色
        /// </summary>
        public void StartWarning()
        {
            //this.timerColorChange.Start();
        }

        public void EndWarning()
        {
            //this.timerColorChange.Stop();
        }

        private void timerColorChange_Tick(object sender, EventArgs e)
        {
            int r = color1.R + (color2.R - color1.R) * count / totalStep;
            int g = color1.G + (color2.G - color1.G) * count / totalStep;
            int b = color1.B + (color2.B - color1.B) * count / totalStep;
            SetTaskColorStyle(Color.FromArgb(r, g, b));

            if (count == totalStep)
            {
                direction = false;
            }
            else if(count == 0)
            {
                direction = true;
            }

            if (direction)
            {
                ++count;
            }
            else
            {
                --count;
            }

        }

        public EbbinghausTask Task
        {
            get { return _task; }
        }

        public CtrlTodoTaskEx(Logic.EbbinghausTask task)
            : this()
        {
            loadTask(task);
        }

        private void loadTask(Logic.EbbinghausTask task)
        {
            _task = task;
            kryptonHeaderGroup1.ValuesPrimary.Heading = task.Subject;
            txtSubject.Text = task.Subject;
            txtComment.Text = task.Comment;
            Tag = task.ID;

            if (_task.NextIndex < 2)
            {
                this.timerColorChange.Interval = 10;
                StartWarning();
            }

        }

        public void SetFinishStatus()
        {
            Color grn = Color.FromArgb(92, 184, 92);

            tableLayoutPanel1.BackColor = grn;
            panel2.StateCommon.Color1 = grn;
            txtSubject.StateCommon.Back.Color1 = grn;
            txtComment.StateCommon.Back.Color1 = grn;
            this.Size = this.kryptonHeaderGroup1.Size;
        }

        public void SetPastTaskStyle()
        {
            Color orange = Color.FromArgb(240, 173, 78);
            tableLayoutPanel1.BackColor = orange;
            panel2.StateCommon.Color1 = orange;
            txtSubject.StateCommon.Back.Color1 = orange;
            txtComment.StateCommon.Back.Color1 = orange;
            this.Size = this.kryptonHeaderGroup1.Size;
        }

        public void SetTaskColorStyle(Color color)
        {
            tableLayoutPanel1.BackColor = color;
            panel2.StateCommon.Color1 = color;
            txtSubject.StateCommon.Back.Color1 = color;
            txtComment.StateCommon.Back.Color1 = color;
            this.Size = this.kryptonHeaderGroup1.Size;

            SetTaskForeColorStyle(Color.White);
        }

        public void SetTaskForeColorStyle(Color color)
        {
            lblSub.ForeColor = color;
            lblNotes.ForeColor = color;
        }

        public event EventHandler UpdateTask
        {
            add
            {
                this.btnFinish.Click += value;
            }
            remove
            {
                this.btnFinish.Click -= value;
            }
        }

        private EbbinghausTask _task;

        private void btnFinish_Click(object sender, EventArgs e)
        {
            EndWarning();
            btnFinish.Visible = false;
            this.txtSubject.Select(0, 0);
        }

        private void buttonSpecHeaderGroup1_ButtonSpecPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.Size = this.kryptonHeaderGroup1.Size;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            FrmUpdateEbbTask updateEbbTask = new FrmUpdateEbbTask(_task);
            if (updateEbbTask.ShowDialog(this.ParentForm) == DialogResult.OK)
            {
                loadTask(EbbTaskDBMgr.GetATask(_task.ID));
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            string subject = _task.Subject;
            string comment = _task.Comment;

            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("主题:" + subject + " " + _task.RemindTimes[(_task.NextIndex - 1) > -1 ? (_task.NextIndex - 1):0]);
            Clipboard.SetText(sBuilder.ToString());
            MessageBox.Show("已复制到剪切板\r\n" + sBuilder.ToString());
        }

        private void btnDelay_Click(object sender, EventArgs e)
        {
            FrmDelay delay = new FrmDelay();
            if (delay.ShowDialog() == DialogResult.OK)
            {
                String unit = delay.Unit;
                int seconds = 1;
                if(unit == "秒") {
                    seconds = 1;
                }
                else if(unit == "分钟") {
                    seconds = 60;
                }
                else if(unit == "小时") {
                    seconds = 3600;
                }

                seconds = delay.Value * seconds;

                GlobalSettings.AddDelayItem(_task.ID, seconds);
            }
        }
    }
}
